//
//  SLMaskView.m
//  shunlianPower2
//
//  Created by shunlian on 2019/1/7.
//  Copyright © 2019 顺联网络. All rights reserved.
//

#import "SLMaskView.h"
#import <QMUIKit/UIViewController+QMUI.h>

@interface SLMaskView ()

@property (nonatomic, copy) dispatch_block_t dismissBlock;

@end

@implementation SLMaskView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        [self addSubview:self.backgroundView];
        [self addSubview:self.contentView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.backgroundView.frame = self.bounds;
}

#pragma mark - Methods

- (void)contentCornerRadius:(CGFloat)cornerRadius {
    self.contentView.layer.cornerRadius = cornerRadius;
    self.contentView.clipsToBounds = YES;
}

- (void)show {
    [self showWithAnimation:YES dismissBlock:nil];
}

- (void)showWithDismissBlock:(nullable dispatch_block_t)dismissBlock {
    [self showWithAnimation:YES dismissBlock:dismissBlock];
}

- (void)showWithAnimation:(BOOL)animation dismissBlock:(nullable dispatch_block_t)dismissBlock {
    [self showInView:[UIApplication sharedApplication].keyWindow animation:animation dismissBlock:dismissBlock];
}

- (void)showInView:(UIView *)view animation:(BOOL)animation dismissBlock:(dispatch_block_t)dismissBlock {
    if (animation) {
        self.alpha = 0;
        [UIView animateWithDuration:0.25 animations:^{
            self.alpha = 1;
        } completion:^(BOOL finished) {
            
        }];
    }
    [view addSubview:self];
    self.dismissBlock = dismissBlock;
}

- (void)dismiss {
    [self dismissWithAnimation:YES complete:nil];
}

- (void)dismissWithComplete:(nullable void (^)(BOOL complete))complete {
    [self dismissWithAnimation:YES complete:complete];
}

- (void)dismissWithAnimation:(BOOL)animation complete:(nullable void (^)(BOOL complete))complete {
    if (animation) {
        [UIView animateWithDuration:0.25 animations:^{
            self.alpha = 0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
            if (complete) {
                complete(YES);
            }
            if (self.dismissBlock) {
                self.dismissBlock();
            }
        }];
    }else {
        if (complete) {
            complete(YES);
        }
        if (self.dismissBlock) {
            self.dismissBlock();
        }
        [self removeFromSuperview];
    }
}

- (void)onTouchAction {
    if (!self.disableBackgroundDismissAction) {
        [self dismiss];
    }
}

- (UIView *)expectSuperView {
    if ([QMUIHelper visibleViewController].navigationController.viewControllers.count == 1) {
        return [UIApplication sharedApplication].delegate.window.rootViewController.view;
    }else {
        return [QMUIHelper visibleViewController].navigationController.view;
    }
}

+ (BOOL)hasVisibleMaskView {
    for (UIView *view in [UIApplication sharedApplication].keyWindow.subviews) {
        if ([view isKindOfClass:[SLMaskView class]]) {
            return YES;
        }
    }
    if ([QMUIHelper visibleViewController].navigationController) {
        for (UIView *view in [QMUIHelper visibleViewController].navigationController.view.subviews) {
            if ([view isKindOfClass:[SLMaskView class]]) {
                return YES;
            }
        }
    }
    for (UIView *view in [QMUIHelper visibleViewController].view.subviews) {
        if ([view isKindOfClass:[SLMaskView class]]) {
            return YES;
        }
    }
    if ([QMUIHelper visibleViewController].tabBarController) {
        for (UIView *view in [QMUIHelper visibleViewController].tabBarController.view.subviews) {
            if ([view isKindOfClass:[SLMaskView class]]) {
                return YES;
            }
        }
    }
    return NO;
}

#pragma mark - Getters

- (UIControl *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIControl alloc] init];
        [_backgroundView addTarget:self action:@selector(onTouchAction) forControlEvents:UIControlEventTouchDown];
        _backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.65];
    }
    return _backgroundView;
}

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = [UIColor whiteColor];
    }
    return _contentView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
